package com.app;

import java.util.List;
import java.util.function.Consumer;

import com.manager.book.Book;
import com.manager.book.BookManager;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import jkoa.core.Context;
import jkoa.core.Next;
import jkoa.middleware.RouterMiddleware;

public class MyRouter extends RouterMiddleware {

	public MyRouter() {

		regist();
	}

	private void regist() {

		get("/", (ctx, next) -> {//注册主页
			
			
			
		});
		post("/api/data", (ctx, next) -> ctx.response.send("Received POST request"));

	}

	@Override
	public void handle(Context ctx, Next next) {

		if (ctx == null) {
			return;
		}
		if (ctx.path() != null && ctx.path().startsWith("/books")) {
			handleBookRoutes(ctx, next);
		} else {
			next.execute(ctx);
		}
	}

	private void handleBookRoutes(Context ctx, Next next) {
		if (ctx.path().equals("/books/add")) {
			// 假设请求体包含 JSON 格式的图书信息：{"title":"新书名","author":"新作者"}
			if ("POST".equals(ctx.method())) {
				String body = (String) ctx.response.getBody();
				// 解析 JSON
				JSONObject jsonObject = new JSONObject(body);
				String title = jsonObject.getStr("title");
				String author = jsonObject.getStr("author");
				int newBookId = generateNewBookId();
				Book newBook = new Book(newBookId, title, author);
				BookManager.me().addBook(newBook);
				ctx.status(201).send("Book added successfully.");
			} else {
				ctx.status(405).send("Method not allowed.");
			}
		} else if (ctx.path().equals("/books/delete")) {
			// 假设请求参数中有 bookId
			if ("DELETE".equals(ctx.method())) {
				int bookId = Integer.parseInt(ctx.response.getBody().toString());
				BookManager.me().deleteBook(bookId);
				ctx.status(200).send("Book deleted successfully.");
			} else {
				ctx.status(405).send("Method not allowed.");
			}
		} else if (ctx.path().equals("/books/update")) {
			// 假设请求体包含 JSON 格式的更新信息：{"id":1,"title":"更新后的书名","author":"更新后的作者"}
			if ("PUT".equals(ctx.method())) {
				String body = (String) ctx.response.getBody();
				JSONObject jsonObject = new JSONObject(body);
				int bookId = jsonObject.getInt("id");
				String newTitle = jsonObject.getStr("title");
				String newAuthor = jsonObject.getStr("author");
				Book updatedBook = new Book(bookId, newTitle, newAuthor);
				BookManager.me().updateBook(bookId, updatedBook);
				ctx.status(200).send("Book updated successfully.");
			} else {
				ctx.status(405).send("Method not allowed.");
			}
		} else if (ctx.path().equals("/books/get")) {
			// 假设请求参数中有 bookId
			if ("GET".equals(ctx.method())) {
				int bookId = Integer.parseInt(ctx.response.getBody().toString());
				Book book = BookManager.me().getBook(bookId);
				if (book != null) {

					ctx.status(200).send(JSONUtil.toJsonStr(book));
				} else {
					ctx.status(404).send("Book not found.");
				}
			} else {
				ctx.status(405).send("Method not allowed.");
			}
		} else {
			ctx.status(404).send("Book route not found");
		}
	}

	// 简单的生成新图书 ID 的方法，实际应用中可根据数据库自增 ID 等方式实现
	private int generateNewBookId() {
		return BookManager.me().getAllBooks().size() + 1;
	}

	// 获取所有图书，用于测试或其他需要获取全部图书的场景
	public static List<Book> getAllBooks() {
		return BookManager.me().getAllBooks();
	}
}